home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS X / Pantomime / Source / Charset.m / Charset.m
Encoding:
Text File  |  2002-04-08  |  2.5 KB  |  144 lines

  1. /*
  2. **  Charset.m
  3. **
  4. **  Copyright (c) 2001, 2002
  5. **
  6. **  Author: Ludovic Marcotte <ludovic@Sophos.ca>
  7. **
  8. **  This library is free software; you can redistribute it and/or
  9. **  modify it under the terms of the GNU Lesser General Public
  10. **  License as published by the Free Software Foundation; either
  11. **  version 2.1 of the License, or (at your option) any later version.
  12. **  
  13. **  This library is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. **  Lesser General Public License for more details.
  17. **  
  18. **  You should have received a copy of the GNU Lesser General Public
  19. **  License along with this library; if not, write to the Free Software
  20. **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22.  
  23. #import <Pantomime/Charset.h>
  24.  
  25. #import <Pantomime/Constants.h>
  26.  
  27. @implementation Charset
  28.  
  29. //
  30. //
  31. //
  32. - (id) initWithCodeCharTable: (const struct charset_code *)c  length: (int)n
  33. {
  34.   self = [super init];
  35.   
  36.   codes = c;
  37.   num_codes = n;
  38.   
  39.   identity_map = 0x20;
  40.   if (n>0 && codes[0].code==0x20)
  41.     {
  42.       int i = 1;
  43.       for (identity_map=0x20;
  44.        i < num_codes && codes[i].code == identity_map + 1 && codes[i].value == identity_map + 1;
  45.        identity_map++,i++) ;
  46.     }
  47.  
  48.   return self;
  49. }
  50.  
  51.  
  52. //
  53. //
  54. //
  55. - (unichar) characterForCode: (int) theCode;
  56. {
  57.   int l, h, m;
  58.   
  59.   if ( theCode <= identity_map )
  60.     {
  61.       return theCode;
  62.     }
  63.  
  64.   l = 0;
  65.   h = num_codes - 1;
  66.   while (l <= h)
  67.     {
  68.       m = (l + h) / 2;
  69.       
  70.       if (codes[m].code == theCode)
  71.     {
  72.       return codes[m].value;
  73.     }
  74.       
  75.       if (codes[m].code > theCode)
  76.     {
  77.       l = m + 1;
  78.     }
  79.       else
  80.     {
  81.       h = m - 1;
  82.     }
  83.     }
  84.  
  85.   /* U+FFFD is supposed to be used for invalid characters */
  86.   return 0xfffd;
  87. }
  88.  
  89.  
  90. //
  91. // TODO: what should this return for eg. \t and \n?
  92. //
  93. - (int) codeForCharacter: (unichar) theCharacter;
  94. {
  95.   int i;
  96.  
  97.   if (theCharacter <= identity_map)
  98.     {
  99.       return theCharacter;
  100.     }
  101.   
  102.   for (i = 0; i < num_codes; i++)
  103.     {
  104.       if (codes[i].value == theCharacter)
  105.     {
  106.       return codes[i].code;
  107.     }
  108.     }
  109.   
  110.   return -1;
  111. }
  112.  
  113.  
  114. //
  115. //
  116. //
  117. - (BOOL) characterIsInCharset: (unichar) theCharacter
  118. {
  119.   if (theCharacter <= identity_map)
  120.     {
  121.       return YES;
  122.     }
  123.  
  124.   if ([self codeForCharacter: theCharacter] != -1)
  125.     {
  126.       return YES;
  127.     }
  128.   
  129.   return NO;
  130. }
  131.  
  132.  
  133. //
  134. // Returns the name of the Charset. Like:
  135. // "iso-8859-1"
  136. // 
  137. - (NSString *) name
  138. {
  139.   [self subclassResponsibility: _cmd];
  140.   return nil;
  141. }
  142.  
  143. @end
  144.